var ns;
app = new Vue({
    el: '#app',
    data: {
        ProjectList: [],//公司列表---总公司和子公司
        CompanyID: 0,//公司ID
        FullName: '',//公司名称
        IsSearch:false,//是否是搜索状态 false不是 true是
        defaultImg:"../image/default.png",//默认头像
        isshow: false,
        childlist: [],
        currentchosenlist: [],//部门列表
        form: {
            keywords: ''
        }
    },
    methods: {
        get_project: function () {
            var that = this;
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                keywords: that.form.keywords,
                action: 'getcompanyorganize',
                companyID: that.CompanyID
            }, function (succeed, data, err) {
                if (succeed) {
                    let resultData = data.data;
                    if (data.status && resultData) {
                        // 公司
                        if (resultData.AllCompany && resultData.AllCompany instanceof Array) {
                            that.ProjectList = resultData.AllCompany;
                            that.FullName = that.ProjectList[0].text;
                            that.CompanyID = that.ProjectList[0].ID;
                            that.initPicker();
                            that.get_data();
                        };
                       
                    };
                } 
            });
        },
        get_data: function() {
            var that = this;
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                keywords: that.form.keywords,
                action: 'getcompanyorganize',
                companyID: that.CompanyID
            }, function(succeed, data, err) {
                that.IsSearch = !that.form.keywords || that.form.keywords.length===0 ? false : true;
                if (succeed) {
                    let resultData = data.data;
                    if(data.status && resultData){
                        // 部门
                        if(resultData.list && resultData.list instanceof Array){
                            that.currentchosenlist = resultData.list;
                        }
                    };
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        tel_phone: function(item) {
            if(!item.PhoneNumber || item.PhoneNumber.length===0){
                api.toast({
                    msg: "该用户还没有添加电话号码",
                    duration: 2000,
                    location: 'bottom'
                });
            }
            else{
                api.call({
                    type: 'tel_prompt',
                    number: item.PhoneNumber
                });
            };
        },
        show_morehistory: function(item) {
            item.isopen = !item.isopen;
        },
        openWin: function(item, params) {
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_search: function() {
            this.get_data();
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    // 选择项目
                    var pickerProject = new $.PopPicker({
                        layer: 1,
                        fileID: 'ID',
                        fileName: 'text',
                    });
                    var projectData = _this.ProjectList || [];
                    pickerProject.setData(projectData);
                    var showProjectPickerButton = doc.getElementById('chooseproject');
                    showProjectPickerButton.addEventListener('click', function (event) {
                        pickerProject.show(function (items) {
                            _this.CompanyID = items[0].value;
                            _this.FullName = items[0].text;
                            _this.get_data();
                            //返回 false 可以阻止选择框的关闭
                            //return false;
                        });
                    }, false);

                });
            })(mui, document);
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.get_project();
};
